/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * Project Name: lmrp-android app
 * Create Time: 16-2-16 下午6:45
 */

package com.lenovo.mso2o.ui.fragment;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.lenovo.framework.base.ActionBarFragment;
import com.lenovo.framework.util.FPreferenceManager;
import com.lenovo.framework.util.ViewHelper;
import com.lenovo.mso2o.R;
import com.lenovo.mso2o.biz.AccountManagerImpl;
import com.lenovo.mso2o.rest.ServiceGenerator;
import com.lenovo.mso2o.rest.UserAPI;

import java.util.ArrayList;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link ShortcutSettingFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 */
public class ShortcutSettingFragment extends ActionBarFragment implements  CompoundButton.OnCheckedChangeListener {
    final public static int SCHEME_QUERY = 1;//方案查询
    final public static int PRODUCTDESCRIPTION = 2;//产品说明
    final public static int FASTBILLING = 3;//快速开单
    final public static int MYMESSAGE = 4;//我的消息
    final public static int INITIATECHAT = 5;//发起聊天
    final public static int HANDYHAVERSACK = 6;//随手袋
    final public static int ADDFUNCTION = 7;//添加
    private boolean changed = false;

    public static String title = "shotcuts";
    private  UserAPI userAPI;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        ArrayList<View> list = ViewHelper.getAllChildren(view);
        userAPI = ServiceGenerator.createService(UserAPI.class);
        int i = 0;
        for(View v : list){
            if(v instanceof CheckBox) {
                ((CheckBox) v).setOnCheckedChangeListener(this);
                //related with ui xml
                ((CheckBox) v).setChecked(FPreferenceManager.getBoolean("" + v.getTag(), false));
            }
        }
        return view;
    }

    public ShortcutSettingFragment() {
        // Required empty public constructor
    }

    @Override
    public int provideContentRes() {
        return R.layout.shortcuts_settings;
    }

    @Override
    protected void onFragmentInVisible(Bundle savedInstanceState) {
        if(changed){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    AccountManagerImpl.syncAppConfigJson(userAPI, true);
                }
            }).start();
        }
    }

    @Override
    protected void onFragmentVisible(Bundle savedInstanceState) {
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        FPreferenceManager.putBoolean("" + buttonView.getTag(), isChecked);
        changed = true;
    }

    @Override
    public int getActionBarRightResourceId() {
        return 0;
    }

    @Override
    public int getActionBarRight2ResourceId() {
        return 0;
    }

    @Override
    public void onActionBarRight2Click() {

    }

    @Override
    public void onActionBarRightClick() {

    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }
}
